<html>

<!-- Mirrored from bos.asmhackers.net/docs/FASM tutorial/chap07.html by HTTrack Website Copier/3.x [XR&CO'2013], Tue, 07 Oct 2014 11:02:26 GMT -->
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<title>07 TAJGA FASM Tutorial</title>
</head>

<body>

<center><b>CHAPTER 7 - Arithmetic instructions, more on flags</b></center><br><br>

In this chapter you will learn how to perform basic math operations in
assembly language. Then you will get deeper into how processor does it and
thus learn more on flags.

<br><br><br>
<a name="1"></a>
<b>7.1. Addition and substraction</b><br>
Simplest case of addition is addition of one, called "incrementing". For
example, if we increment variable holding value 5, it will contain 6 etc.

<br><br>
Instruction which performs incrementing is <code>inc</code> (obvious why). It
has one operand, which tells what should be incremented (eg. to what will 1 be
added). Operand can be register or memory variable. It can't be constant, of
course, because such instruction, even if it would exist, wouldn't have any
effect. Example of incrementing:

<blockquote class="code"><pre>
mov ax<font color=#333399>,</font><font color=#339933>5</font>
inc ax	     <font color=#777777>;increment (add 1 to) value in ax</font>
  <font color=#777777>;here ax holds value 6</font>
</pre></blockquote>

<br>
I think it is clear (it isn't, you'll see later why).

<br><br>
Substracting of value 1 is called "decrementing". Decrementing is opposite of incrementing. Instruction which
performs decrementing is <code>dec</code>. Example:

<blockquote class="code"><pre>
mov ax<font color=#333399>,</font><font color=#339933>5</font>
inc ax	     <font color=#777777>;increment (add 1 to) value in ax</font>
  <font color=#777777>;here ax holds value 6</font>
dec ax	     <font color=#777777>;decrement (substract 1 from) value in ax</font>
  <font color=#777777>;here ax holds value 5 again</font>
</pre></blockquote>

<blockquote class="term">
terms: <b>incrementing</b> (adding 1), <b>decrementing</b> (substracting 0)<br>
instructions: <b>inc</b>, <b>dec</b>
</blockquote>

If you wan't to add or substract more than 1, you can use more
<code>inc</code>s or <code>dec</code>s, but that is quite ugly way, requires
more typing, and code is big and slow. So there is instruction which can add
any value, this instruction is <code>add</code>. It takes two arguments, first
one is destination eg. value to which will be added, and second is value to be
added.  Argument types are same as for <code>mov</code>, first can be register
or memory variable, second can be constant, register or memory variable (if
first one isn't memory variable, remember - one instruction can't access two
memory locations). Example:

<blockquote class="code"><pre>
mov ax<font color=#333399>,</font><font color=#339933>5</font>
add ax<font color=#333399>,</font><font color=#339933>5</font>
<font color=#777777>;here ax contains 10</font>
</pre></blockquote>

<br>
Another example:

<blockquote class="code"><pre>
mov ax<font color=#333399>,</font><font color=#339933>5</font>
mov bx<font color=#333399>,</font><font color=#339933>5</font>
add bx<font color=#333399>,[</font>five<font color=#333399>]</font>
add ax<font color=#333399>,</font>bx
<font color=#777777>;here ax contains 15, bx contains 10</font>
five dw <font color=#339933>5</font>
</pre></blockquote>

<br>
Instruction for substracting is <code>sub</code>. It is exact opposite of
<code>add</code>, everything is same for it as for <code>add</code>.

<blockquote class="code"><pre>
mov ax<font color=#333399>,</font><font color=#339933>15</font>
mov bx<font color=#333399>,</font><font color=#339933>10</font>
sub bx<font color=#333399>,[</font>five<font color=#333399>]</font>
sub ax<font color=#333399>,</font>bx
<font color=#777777>;here ax contains 10, bx contains 5</font>
five dw <font color=#339933>5</font>
</pre></blockquote>

<br><br><br>
<a name="2"></a>
<b>7.2. Overflows</b>

<br>
There are some cases with addition and substraction that I haven't already
mentioned. For example if you try to add 10 to byte sized variable containing
250 (biggest number byte sized variable can hold is 255). In such case, we say
that <code>overflow</code> has occured.

<br><br>
But question is, what happens with result of operation that has overflown. When
upper limit of variable is crossed, then result of operation will be rest of
value to be added. We can say the operation will be "wrapped" from max value to
minimal value. For example: <br>
byte 255 + 1 = 0<br>
byte 255 + 2 = 1<br>
byte 254 + 3 = 1<br>
byte 250 + 10 = 5<br>
byte 255 + 255 = 254<br>
word 65535 + 1 = 0<br>
word 65535 + 65535 = 65534<br>
etc.

<br><br>
There is also other case, when result of operation falls below lower limit
(which is 0 for any size of variable). In this case result of operation will be
wrapped from lower limit to upper limit. This case is called
<code>underflow</code>. For example:<br>
byte 0 - 1 = 255<br>
byte 0 - 255 = 1<br>
byte 254 - 254 = 0<br>
byte 254 - 255 = 255<br>
etc.

<br><br>
<b>NOTE:</b> Word <code>oveflow</code> is usually used for both
<code>overflow</code> and <code>underflow</code>.

<blockquote class=term>
Terms: <b>Overflow</b>, <b>Underflow</b>
</blockquote>

We also need to know how to check if overflow has occured after performing
operation to prevent bugs.  For this, flags are used. I mentioned flags in 
<a href=chap05.html#3>chapter 5.3</a>. We used flags for checking results of
comparison at conditional jumps, and I also said that there shouldn't be any
instrcutions between comparison and jumps, because many instructions change
flags (of course you can place instruction there if you are sure it won't
change any needed flag). Arithmetic instructions <code>add</code> and
<code>sub</code> use one bit of flags called CF (carry flag). If overflow
occurs, then they set it to 1, otherwise they set it to 0. You can test carry
flag with conditional jumps <code>jc</code> and <code>jnc</code> (see 
<a href=chap05.html#3>chapter 5.3</a> about conditional jumps). <code>jc</code> jumps
if carry flag is set, <code>jnc</code> jumps if carry flag is not set.

<br><br>
Here is example of testing overflows:
<blockquote class="code"><pre>
add ax<font color=#333399>,</font>bx
jc overflow
no_overflow<font color=#333399>:</font>

sub cx<font color=#333399>,</font>dx
jc underflow
no_underflow<font color=#333399>:</font>
</pre></blockquote>

<blockquote class="term">
<b>carry flag (CF)</b> - ont bit (flag) of "flags" register<br>
conditional jump instructions: <b>jc</b>, <b>jnc</b>
</blockquote>

<br><br><br>
<a name="3"></a>
<b>7.3. Zero Flag</b>

<br>
Instructions <code>inc</code> and <code>dec</code> doesn't set CF, so you
can't test overflow using CF with them. But there is another rule that can be
used to prevent overflow with <code>inc</code> and <code>dec</code>. This rule
is that when result of operation is zero, then flag called "zero flag" (ZF) is
set. This flag is tested with <code>jz</code> (jump if zero flag is set) and
<code>jnz</code> (jump if zero flag is clear) conditional jump instructions.

<br><br>
With this you can create loops, eg. repeat some part of code several times.<br>
For example code
<blockquote class="code"><pre>
  org <font color=#339933>256</font>
  mov cx<font color=#333399>,</font><font color=#339933>5</font>
here<font color=#333399>:</font>
  mov dl<font color=#333399>,<font color=#bb0000>'a'</font></font>
  mov ah<font color=#333399>,</font><font color=#339933>2</font>
  int <font color=#339933>21h</font>
  dec cx
  jnz here

  int <font color=#339933>20</font>h
</pre></blockquote>
will write
<blockquote class="console">
aaaaa
</blockquote>

<b>NOTE:</b> You can optimize that part of code to
<blockquote class="code"><pre>
  org <font color=#339933>256</font>
  mov cx<font color=#333399>,</font><font color=#339933>5</font>
  mov dl<font color=#333399>,<font color=#bb0000>'a'</font></font>
  mov ah<font color=#333399>,</font><font color=#339933>2</font>
here<font color=#333399>:</font>
  int <font color=#339933>21h</font>
  dec cx
  jnz here

  int <font color=#339933>20</font>h
</pre></blockquote>
because value of <code>dl</code> and <code>ah</code> isn't changed anywhere in loop, so
we don't have to set it each time.

<br><br>
Not only <code>add</code> and <code>sub</code> instructions set ZF if result is
zero (and clear it otherwise). All basic arithmetic instructions do this. For
now, you know these arithmetic instructions: <code>add</code>,
<code>sub</code>, <code>and</code>, <code>xor</code> and <code>or</code>. So
afer any of these instruction, ZF tells you if destination (first argument) of
operation holds 0. For example, You can use this behavior to check if value of
register is 0. Before, you would do this with

<blockquote class="code"><pre>
cmp ax<font color=#333399>,</font><font color=#339933>0</font>
jz ax_is_zero
</pre></blockquote>

but you can also use "or" to do this:

<blockquote class="code"><pre>
or ax<font color=#333399>,</font>ax
jz ax_is_zero
</pre></blockquote>

Or won't change <code>ax</code>, because 1 ored with 1 is 1, and 0 ored with 0
is 0. Reread <a href="chap06.html">chapter 6</a> if you don't comprehend that.
Btw, this was used on older computers because such code was faster and is few
bytes smaller than with <code>cmp</code>.

<br><br><br>
<a name="4"></a>
<b>7.4. Carry flag, more binary arithmetic instructions</b>

<br>
I mentioned carry flag a little in connection with overflow. But CF is really general-purpose
flag because it can be tested easily (<code>jc</code>,<code>jnc</code> and few more), and
it's value can be easily set. You will find many more uses of CF later.

<br><br>
How to set CF? There are two instructions for this. <code>stc</code> stays for
"SeT Carry", and so it "sets" carry flag (eg. sets it's value to 1), so
<code>jc</code> jump is taken and <code>jnc</code> is not taken etc etc, you
should understand this aleady. Instruction <code>clc</code> (CLear Carry)
clears CF.

<br><br>
When we know how to work with CF, we can learn about rest of bit arihmetic
operations. First will be <code>shl</code>. It shift bits of register to left,
eg. 0th bit becomes 1st, 1st becomes 2nd etc. Last bit (7th of byte or 15th of
word) is moved to CF. First bit becomes 0. This way (if highest bit wa zero) we
have multiplied the shifted register by 2.

<br><br>
Before shifting: <code>bit7:bit6:bit5:bit4:bit3:bit2:bit1:bit0</code><br>
After shifting: <code>bit6:bit5:bit4:bit3:bit2:bit1:bit0:0</code>,  <code>CF=bit7</code>

<br><br>
To explain why number is multipied by 2. If you remember beginning of 
<a href=chap06.html>chapter 6</a>, you know that number before shifting is
<code>128*bit7 + 64*bit6 + 32*bit5 + 16*bit4 + 8*bit3 + 4*bit2 + 2*bit1 +
bit0</code> so after shifting it becomes <code>128*bit6 + 64*bit5 + 32*bit4 +
16*bit3 + 8*bit2 + 4*bit1 + 2*bit0</code> which is <code>2*(64*bit6 + 32*bit5 +
16*bit4 + 8*bit3 + 4*bit2 + 2*bit1 + bit0)</code>. So if highest bit is zero,
then number is multiplied by two. This way we can easily multiply by powers of
two (2, 2^2=4, 2^3=8, 2^4=16 etc.). Also upper bit is stored in CF so we can
test overflow of mulitplication with <code>jc</code> and <code>jnc</code>

<br><br>
Usually we want to shift more than once (multiply by 4, 8, 16...), so
<code>shl</code> takes second argument, which tells how many times we want to
shift. If we shift by number greater than 1, CF contains 1 if ANY of discarded
bits (x highest bits, where x is value we are shifting by) contained 1. This
way we can still check for overflow. If you are beginner, don't care about
overflow checking too much, you probably won't do it anyway :) (and your
program will probably contains bugs then)

<br><br>
There is one limitation to <code>shl</code> - it's arguments doesn't follow same
rules as other instructions you know (<code>mov</code>,<code>add</code> etc.)
Fisrt argument can be register or memory location, but second can be only numeric
constant or CL register (really, no other).

<br><br>
<b>NOTE:</b> Orignially, at 8086 (that's 086, first of 80x86 series known as
x86, like 286 or 486), there was only <code>shl</code> instruction which could
shift by one, and so for example <code>shl ax,3</code> was compiled into 3
<code>shl</code>s. There also wasn't any shifting by register, you had to make
loop for that. Fortunately 80286 had shifting by constant and by CL so it is
now OK.

<br><br>
That was about shifting left, but there is also other type of shifting, that is
shifting to right. I hope you can now imagine what it does, just few notes to
it. Instruction that performs this is <code>shr</code> (shift right). It's
effect is division by two (or powers of two) without remainder. If we shift
right by two, then remainder (0 or 1) is in CF, otherwise CF beheaves like with
shifting left by number higher than two: If remainder isn't 0 (at least one of
discarded bits was 1) then CF is set, otherwise it is clear.

<br><br><br>
<a name="5"></a>
<b>7.5. Some examples</b>

<br>
At least we are able to make output of number (write number to screen). Too bad
we can only write it in binary :). So here is our task: Write program that
outputs any binary number. For now, we will hardcode number into program, eg.
<code>mov</code>e it into some register as constant. Here is the source.

<blockquote class="code"><pre>
org <font color=#339933>100h</font>

mov bx<font color=#333399>,</font><font color=#339933>65535</font> <font color=#777777>;we store number we want to display in bx</font>
 <font color=#777777>;(because it's not used by DOS services we use)</font>
mov cx<font color=#333399>,</font><font color=#339933>16</font> <font color=#777777>;we are diplaying 16 digits (bits)</font>

<font color=#777777>;display one digit from BX each loop</font>

display_digit<font color=#333399>:</font>
shl bx<font color=#333399>,</font><font color=#339933>1</font>
jc display_one

<font color=#777777>;display '0'</font>
mov ah<font color=#333399>,</font><font color=#339933>2</font>
mov dl<font color=#333399>,<font color=#bb0000>'0'</font></font>
int <font color=#339933>21h</font>
jmp continue

<font color=#777777>;display '1'</font>
display_one<font color=#333399>:</font>
mov ah<font color=#333399>,</font><font color=#339933>2</font>
mov dl<font color=#333399>,<font color=#bb0000>'1'</font></font>
int <font color=#339933>21h</font>

<font color=#777777>;check if we want to continue</font>
continue<font color=#333399>:</font>
dec cx
jnz display_digit

<font color=#777777>;end program</font>
int <font color=#339933>20h</font>
</pre></blockquote>


<br>
I hope you understand this, it's quite simple. Each loop we shift BX register
left by one, so upper bit is moved to CF, then we write '0' or '1' depending 
on value of CF (previously upper bit of number) and continue loop until we write
16 digits (because word has 16 bits).

<br><br>
Example of stepping thru code:<br>
<code>
Start: CF=16, BX=1100101000001011b<br>
Pass1: CX=15, BX=1001010000010110b, CF=1<br>
Pass2: CX=14, BX=0010100000101100b, CF=1<br>
Pass3: CX=13, BX=0101000001011000b, CF=0<br>
...<br>
Pass14: CX=2, BX=1100000000000000b, CF=0<br>
Pass15: CX=1, BX=1000000000000000b, CF=1<br>
Pass16: CX=0, BX=0000000000000000b, CF=1<br>
</code>

<br><br>
In my opinion, if you made it here, with (generaly) understing everything, you
can consider yourself to be more than beginner, congratulations!!! There is
much to learn to become well-armed assembly programmer, but you now have the
sufficent base which you will only extend, with or without use of this
tutorial. (But there are several parts which will be explained further which
are hard to find in any tutorial)

</body>

<!-- Mirrored from bos.asmhackers.net/docs/FASM tutorial/chap07.html by HTTrack Website Copier/3.x [XR&CO'2013], Tue, 07 Oct 2014 11:02:26 GMT -->
</html>
